package de.nonspectare.utils;

import java.io.File;
import java.io.FileWriter;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.Types;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;


public class WSDLReader {

	private static final Logger logger = Logger.getLogger("Logging");

	public static void changeTargetNamespace(String file,
			String targetNamespace, String location, String name) {

		// Quelle: http://www.java.happycodings.com/Other/code24.html

		try {
			// get hold the WSDLFactory
			WSDLFactory factory = WSDLFactory.newInstance();

			// create an object to read the WSDL file
			javax.wsdl.xml.WSDLReader reader = factory.newWSDLReader();

			// pass the URL to the reader for parsing and get back a WSDL
			// definiton
			Definition wsdlInstance = reader.readWSDL(null, file);
			
			// get a map of the five specific parts a WSDL file
//			Types types = wsdlInstance.getTypes();
//			Map messages = wsdlInstance.getMessages();
//			Map portTypes = wsdlInstance.getPortTypes();
//			Map bindings = wsdlInstance.getBindings();
//			Map services = wsdlInstance.getServices();

			wsdlInstance.setTargetNamespace(targetNamespace);
			
//			wsdlInstance.

			writeWSDLFile(wsdlInstance, location, name);

			/**
			 * perform lookup of types, messages, portTypes, bindings and
			 * services
			 */
		} catch (WSDLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void writeWSDLFile(Definition wsdlInstance, String location,
			String name) {

		try {
			// get hold the WSDLFactory
			WSDLFactory factory = WSDLFactory.newInstance();

			// create an object to read the WSDL file
			javax.wsdl.xml.WSDLWriter writer = factory.newWSDLWriter();

			// System.out.println(System.getProperty("user.dir"));

			writer.writeWSDL(wsdlInstance, new FileWriter(location + "//"
					+ name));
		}

		catch (Exception e) {
			e.printStackTrace();
			logger.log(Level.WARNING, "File could not write!", e);
		}
		logger.fine("New WSDL file was written");

	}

	public static void getAllOperations(File wsdlFile, String operationName) {
		try {
			// get hold the WSDLFactory
			WSDLFactory factory = WSDLFactory.newInstance();

			// create an object to read the WSDL file
			javax.wsdl.xml.WSDLReader reader = factory.newWSDLReader();

			// pass the URL to the reader for parsing and get back a WSDL
			// definiton
			Definition wsdlInstance = reader.readWSDL(null, wsdlFile.getAbsolutePath());
			
			String test = wsdlInstance.getAllServices().get(0).toString();
			System.out.println(test);
			
			
			wsdlInstance.getAllBindings();
			wsdlInstance.getAllPortTypes();
			wsdlInstance.getAllServices();

//			Service service = wsdlInstance.getService()
//					wsdlInstance.getPortType(new QName(operationName));
//			
//			Port port 
//			
//			System.out.println(portTypes.get(0));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}